<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      color: rgb(231, 86, 29);
    }
  </style>
</head>

<body>
  <div id="app">
    <router-link to="/home">首页</router-link>
    <router-link :to="{
      path: '/news',
      query: {
        id: 1,
        page: 2
      }
    }">新闻页</router-link>
    <router-link to="/about" replace>关于我们页</router-link>
    <hr>
    <!-- 视图上定义 router-view 作为路由组件的出口 -->
    <router-view></router-view>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./vue-router.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp();
    // 定义好路由组件的 config
    const HomePage = {
      template: `
        <div>
          <h1>这是首页</h1>  
        </div>
      `
    }
    const NewsPage = {
      template: `
        <div>
          <h1>这是新闻页</h1>  
        </div>
      `,
      mounted() {
        console.log(this.$route);
      }
    }
    const AboutPage = {
      template: `
        <div>
          <h1>这是关于我们页</h1>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h1>阿欧，404啦</h1>  
        </div>
      `
    }
    const LoginPage = {
      template: `
        <div>
          <h1>登录页</h1>  
          <button @click="userLogin">点击登录</button>
        </div>
      `,
      methods: {
        userLogin() {
          setTimeout(() => {
            const token = 'dwfeg-fgrth-sert';
            localStorage.setItem('token', token);
            // 跳回首页
            this.$router.replace('/home')
          }, 1000)
        }
      },
    }
    // 创建路由实例
    const routes = [
      // 单个路由规则 至少有两个属性  path 路径 component 当前路由对应路由组件
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: HomePage
      },
      {
        path: '/news',
        name: 'news',
        component: NewsPage
      },
      {
        path: '/about',
        name: 'about',
        component: AboutPage
      },
      {
        path: '/login',
        name: 'login',
        component: LoginPage
      },
      {
        path: '/:pathMatch(.*)*',
        component: NotFound
      }
    ]
    const router = VueRouter.createRouter({
      // 路由模式，这里使用的是hash 模式
      history: VueRouter.createWebHashHistory(),
      // 路由规则
      routes
    })
    const isLogin = () => !!localStorage.getItem('token');
    // 使用next拦截
    router.beforeEach((to, from, next) => {
      // 如果页面都需要登录才能访问 一般 有几个一定放行 登录 注册
      const passRoutes = ['/login', '/reg'];
      if (passRoutes.includes(to.path)) {
        next();
      } else {
        if (isLogin()) {
          // 登录了
          next();
        } else {
          next('/login');
        }
      }


    })
    // 使用 returnfalse拦截
    // 使用路由插件
    app.use(router);
    app.mount('#app')

  </script>
</body>

</html>